package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.service.SetmealService;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.*;

@RequestMapping("/report")
public class ReportController {
    @Reference
    private SetmealService setmealService;
   @RequestMapping("/getSetmealReport")
   public Result getSetmealReport(){
       List<Map<String,Object>> list = setmealService.findSetmealCount();
       Map<String,Object> map = new HashMap<>();
       map.put("setmealCount",list);
       List<String> setmealNames = new ArrayList<>();
       for(Map<String,Object> m :list){
           String name = (String) m.get("name");
           setmealNames.add(name);
       }
       map.put("setmealNames",setmealNames);
       return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
   }


    @Reference
    private MemberService memberService;
    /*
    * 会员统计数量
    *
    * */
    @RequestMapping("/getMemberReport")
    public Result getMemberReport(){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,-12);//获取当前日期之前12个月的日期
        List<String> list = new ArrayList<>();
        for(int i = 0;i<12;i++){
            calendar.add(Calendar.MONTH,1);
            list.add(new SimpleDateFormat("yyyy,MM").format(calendar.getTime()));
        }
        Map<String,Object> map = new HashMap<>();
        map.put("months",list);
        List<Integer> memberCount = memberService.findMemberCountByMonth(list);
map.put("memberCount",memberCount);
return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);

    }
}
